﻿using System;
using System.Text;
using System.Diagnostics;

namespace AudioCore.Common
{
    /// <summary>
    /// Provides a set of methods and properties that enable applications to trace
    /// <para>the execution of code and associate trace messages with their source.</para>
    /// </summary>
    public class TraceLog
    {
        private TraceSource innerTraceSource = null;
        public TraceSource TraceSource
        {
            get { return innerTraceSource; }
        }

        public TraceLog(string name, SourceLevels level)
        {
            try
            {
                innerTraceSource = new TraceSource(name, level);
            }
            catch (ArgumentException)
            {
                //Do not throw this exception to outside
                innerTraceSource = null;
            }
        }

        /// <summary>
        /// Close the inner trace source
        /// </summary>
        public void Close()
        {
            Dispose();
        }

        /// <summary>
        /// Dispose the inner trace source
        /// </summary>
        public void Dispose()
        {
            if (innerTraceSource != null)
            {
                innerTraceSource.Close();
                innerTraceSource = null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="id"></param>
        /// <param name="message"></param>
        public void TraceEvent(TraceEventType eventType, int id, string message)
        {
            try
            {
                if (innerTraceSource != null)
                {
                    StringBuilder header = new StringBuilder();
                    StackFrame stackFrame = new StackTrace(true).GetFrame(1);
                    System.Reflection.MethodBase mb = stackFrame.GetMethod();

                    switch (eventType)
                    {
                        case TraceEventType.Start:
                        case TraceEventType.Stop:
                        case TraceEventType.Suspend:
                        case TraceEventType.Resume:
                            {
                                header.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffffff"));
                                break;
                            }
                        default:
                            {
                                header.AppendFormat(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
                                break;
                            }
                    }
                    header.AppendFormat(", [{0}.{1}()], {2}", mb.ReflectedType.Name, mb.Name, message);
                    innerTraceSource.TraceEvent(eventType, id, header.ToString());
                    innerTraceSource.Flush();
                    innerTraceSource.Close();
                }
            }
            catch (Exception)
            {
                //Do not throw this exception to outside
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="id"></param>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void TraceEvent(TraceEventType eventType, int id, string format, params object[] args)
        {
            try
            {
                if (innerTraceSource != null)
                {
                    StringBuilder header = new StringBuilder();
                    StackFrame stackFrame = new StackTrace(true).GetFrame(1);
                    System.Reflection.MethodBase mb = stackFrame.GetMethod();

                    switch (eventType)
                    {
                        case TraceEventType.Start:
                        case TraceEventType.Stop:
                        case TraceEventType.Suspend:
                        case TraceEventType.Resume:
                            {
                                header.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffffff"));
                                break;
                            }

                        default:
                            {
                                header.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
                                break;
                            }
                    }
                    header.AppendFormat(", [{0}.{1}()], {2}", mb.ReflectedType.Name, mb.Name, format);
                    innerTraceSource.TraceEvent(eventType, id, header.ToString(), args);
                    innerTraceSource.Flush();
                    innerTraceSource.Close();
                }
            }
            catch (Exception)
            {
                //Do not throw this exception to outside
            }
        }

        public void TraceInformation(string message)
        {
            try
            {
                if (innerTraceSource != null)
                {
                    StringBuilder header = new StringBuilder();
                    StackFrame stackFrame = new StackTrace(true).GetFrame(1);
                    System.Reflection.MethodBase mb = stackFrame.GetMethod();

                    header.AppendFormat(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
                    header.AppendFormat(", [{0}.{1}()], {2}", mb.ReflectedType.Name, mb.Name, message);
                    innerTraceSource.TraceEvent(TraceEventType.Information, 0, header.ToString());
                    innerTraceSource.Flush();
                    innerTraceSource.Close();
                }
            }
            catch (Exception)
            {
                //Do not throw this exception to outside
            }
        }

        public void TraceError(string message)
        {
            try
            {
                if (innerTraceSource != null)
                {
                    StringBuilder header = new StringBuilder();
                    StackFrame stackFrame = new StackTrace(true).GetFrame(1);
                    System.Reflection.MethodBase mb = stackFrame.GetMethod();

                    header.AppendFormat(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
                    header.AppendFormat(", [{0}.{1}()], {2}", mb.ReflectedType.Name, mb.Name, message);
                    innerTraceSource.TraceEvent(TraceEventType.Error, 0, header.ToString());
                    innerTraceSource.Flush();
                    innerTraceSource.Close();
                }
            }
            catch (Exception)
            {
                //Do not throw this exception to outside
            }
        }

        public void TraceDebug(string message)
        {
            try
            {
                if (innerTraceSource != null)
                {
                    StringBuilder header = new StringBuilder();
                    StackFrame stackFrame = new StackTrace(true).GetFrame(1);
                    System.Reflection.MethodBase mb = stackFrame.GetMethod();

                    header.AppendFormat(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
                    header.AppendFormat(", [{0}.{1}()], {2}", mb.ReflectedType.Name, mb.Name, message);
                    innerTraceSource.TraceEvent(TraceEventType.Verbose, 0, header.ToString());
                    innerTraceSource.Flush();
                    innerTraceSource.Close();
                }
            }
            catch (Exception)
            {
                //Do not throw this exception to outside
            }
        }
    }
}
